package com.qingmi.farmer.data;

import android.content.Context;
import android.text.TextUtils;

import com.qingmi.farmer.R;
import com.qingmi.farmer.viewmodel.GoodsOrderItemViewModel;
import com.wjjun.basemvpvm.databinding.adapter.ImageViewAdapter;

/**
 * 数据设定
 * Created by wjjun on 2018/4/26.
 */

public class DataSetting {

    //服务器域名
    //正式服务器
//    public static final String SERVER_HOST = "api.qmilife.com.cn";
    //测试服务器
    public static final String SERVER_HOST = "api.develop.qmilife.com.cn";
    //内外测试网
//    public static final String SERVER_HOST = "192.168.19.215";

    //服务器端口
//    正式服务器
    public static final int SERVER_PORT = 80;
//    public static final int SERVER_PORT = 805;

    //服务器api路径
    public static final String SERVER_PATH = "";
//    public static final String SERVER_PATH = "api/";

    //图片服务器地址
    private static String PIC_HOST = "http://www.qmilife.com.cn:801/img/";
//    private static String PIC_HOST = "http://192.168.15.215:801/img/";

    /**
     * 获取图片地址
     */
    public static String getPicUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return "";
        }
        return PIC_HOST + url;
    }

    public static void initPicHost() {
        ImageViewAdapter.setBaseUrl(PIC_HOST);
    }

    public static void setPicHost(String host) {
        PIC_HOST = host;
        initPicHost();
    }

    /**
     * 性别
     */
    //男
    public static final int GENDER_MAN = 1;
    //女
    public static final int GENDER_WOMAN = 2;

    //true为男, false为女
    public static int getGender(boolean check) {
        return check ? GENDER_MAN : GENDER_WOMAN;
    }

    public static String getGenderMsg(Context context, int gender) {
        if (gender == GENDER_WOMAN) {
            return context.getString(R.string.txt_ms);
        } else if (gender == GENDER_MAN) {
            return context.getString(R.string.txt_sir);
        } else {
            return context.getString(R.string.txt_unknown);
        }
    }

    /**
     * 商品类型
     */
    //生资
    public static final int GOODS_TYPE_GOODS = 1;
    //种植服务
    public static final int GOODS_TYPE_SERVICE = 2;
    //农机具
    public static final int GOODS_TYPE_MACHINERY = 3;

    /**
     * 商品
     */
    //排序  1 价格升序 2 价格降序 3 销量升序 4 销量降序
    //价格升序
    public static final int GOODS_SORT_DEFAULT = 0;
    public static final int GOODS_SORT_PRICE_ASC = 1;
    public static final int GOODS_SORT_PRICE_DESC = 2;
    public static final int GOODS_SORT_SELL_ASC = 3;
    public static final int GOODS_SORT_SELL_DESC = 4;

    /**
     * 生资订单
     */
    //待审核
    public static final int GOODS_ORDER_STATUS_PENDING_CHECK = 2;
    //待确认
    public static final int GOODS_ORDER_STATUS_PENDING_CONFIRM = 3;
    //已确认
    public static final int GOODS_ORDER_STATUS_CONFIRMED = 4;

    /**
     * 执行情况
     */
    //取消
    public static final int GOODS_EXECUTE_STATUS_CANCEL = -1;
    //待付款
    public static final int GOODS_EXECUTE_STATUS_PENDING_PAY = 0;
    //待确转
    public static final int GOODS_EXECUTE_STATUS_PENDING_TRAN = 1;
    //待发货
    public static final int GOODS_EXECUTE_STATUS_PENDING_SEND = 2;
    //待收货
    public static final int GOODS_EXECUTE_STATUS_PENDING_RECEIVE = 3;
    //已完成
    public static final int GOODS_EXECUTE_STATUS_COMPLETED = 4;


    /**
     * 支付方式
     */
    public static final int GOODS_ORDER_PAY_METHOD_ONLINE = 1;
    //待付款
    public static final int GOODS_ORDER_PAY_METHOD_OFFLINE = 2;

    /**
     * 粮食订单
     */
    //已取消
    public static final int GRAIN_ORDER_STATUS_CANCEL = -1;
    //待上报
    public static final int GRAIN_ORDER_STATUS_PENDING_REPORTED = 1;
    //待确认
    public static final int GRAIN_ORDER_STATUS_PENDING_CONFIRM = 2;
    //已完成
    public static final int GRAIN_ORDER_STATUS_COMPLETED = 3;


    //交易类型 1-上架，2-售卖，3-下架，4-交易取消
    public static final int GRAIN_TRADE_TYPE_PUBLISH = 1;
    public static final int GRAIN_TRADE_TYPE_SELL = 2;
    public static final int GRAIN_TRADE_TYPE_WITHDRAW = 3;


    /**
     * 合作社签约状态
     */
    //待审核
    public static final int COOPERATIVE_SIGNED_STATUS_UNSIGNED = 0;
    //审核通过
    public static final int COOPERATIVE_SIGNED_STATUS_SIGNED = 1;
    //审核失败
    public static final int COOPERATIVE_SIGNED_STATUS_FAIL = 2;
    //被解约
    public static final int COOPERATIVE_SIGNED_STATUS_RESCIND = 3;
    //解约审核中
    public static final int COOPERATIVE_SIGNED_STATUS_RESCIND_CHECK = 4;

    /**
     * 运送方式
     */
    //上门
    public static final int DELIVERY_TYPE_DROP_IN = 1;
    //自运
    public static final int DELIVERY_TYPE_SELF = 2;
    //上门, 以农民仓库地址为准
    public static final int DELIVERY_TYPE_DROP_IN_BY_FARMER = 3;

    /**
     * 账单类型
     */
    //全部
    public static final int BILL_LOG_TYPE_ALL = 0;
    //体现
    public static final int BILL_LOG_TYPE_WITHDRAW = 1;
    //充值
    public static final int BILL_LOG_TYPE_TOP_UP = 2;
    //商品收入
    public static final int BILL_LOG_TYPE_SELL = 3;
    //商品支出
    public static final int BILL_LOG_TYPE_BUY = 4;
    //退款收入
    public static final int BILL_LOG_TYPE_REFUND = 5;
    //其他
    public static final int BILL_LOG_TYPE_OTHER = 6;

    /**
     * 贷款状态
     */
    //不存在
    public static final int LOAN_STATUS_NULL = -1;
    //申请中
    public static final int LOAN_STATUS_APPLY = 0;
    //申请后内部审批中
    public static final int LOAN_STATUS_CHECK = 1;
    //内部审批通过银行审批中
    public static final int LOAN_STATUS_BANK_CHECK = 2;
    //银行已批准等待放款
    public static final int LOAN_STATUS_PENDING_LOAN = 3;
    //已放款
    public static final int LOAN_STATUS_FUND = 4;
    //已还款
    public static final int LOAN_STATUS_REFUND = 5;
    //确认坏账
    public static final int LOAN_STATUS_BAD = 6;

    @SuppressWarnings("ConstantConditions")
    public static String getGoodsOrderStatusMsg(Context context, GoodsOrderItemViewModel itemViewModel) {
        if (itemViewModel == null || itemViewModel.status.get() == null) {
            return "";
        }
        switch (itemViewModel.status.get()) {
            case GOODS_ORDER_STATUS_PENDING_CHECK:
                return context.getString(R.string.status_goods_order_check);
            case GOODS_ORDER_STATUS_PENDING_CONFIRM:
                return context.getString(R.string.status_goods_order_confirm);
            case GOODS_ORDER_STATUS_CONFIRMED:
                if (itemViewModel.alreadyCount.get() == 0) {    //全部未完成
                    return context.getString(R.string.status_goods_order_confirmed);
                } else if (itemViewModel.alreadyCount.get() >= itemViewModel.count.get()) {   //完成全部
                    return context.getString(R.string.status_goods_order_confirmed_completed);
                } else {//部分完成
                    return context.getString(R.string.status_goods_order_confirmed_part);
                }
        }
        return "";
    }


    public static String getGoodsOrderPayType(Context context,int payTypeId ) {
        switch (payTypeId) {
            case GOODS_ORDER_PAY_METHOD_ONLINE:
                return context.getString(R.string.pay_method_online);
            case GOODS_ORDER_PAY_METHOD_OFFLINE:
                return context.getString(R.string.pay_method_offline);
        }

        return "";
    }

    public static String getGoodsOrderExecuteMsg(Context context, int status) {
        switch (status) {
            case GOODS_EXECUTE_STATUS_CANCEL:
                return context.getString(R.string.status_goods_order_cancel);
            case GOODS_EXECUTE_STATUS_PENDING_PAY:
                return context.getString(R.string.status_goods_order_pay);
            case GOODS_EXECUTE_STATUS_PENDING_TRAN:
                return context.getString(R.string.status_goods_order_pay_confirm);
            case GOODS_EXECUTE_STATUS_PENDING_SEND:
                return context.getString(R.string.status_goods_order_send);
            case GOODS_EXECUTE_STATUS_PENDING_RECEIVE:
                return context.getString(R.string.status_goods_order_receive);
            case GOODS_EXECUTE_STATUS_COMPLETED:
                return context.getString(R.string.status_goods_order_completed);
        }
        return "";
    }

    public static String getGoodsOrderExecuteDesc(Context context, int status) {
        switch (status) {
            case GOODS_EXECUTE_STATUS_PENDING_PAY:
                return context.getString(R.string.status_goods_order_pay_desc);
            case GOODS_EXECUTE_STATUS_PENDING_TRAN:
                return context.getString(R.string.status_goods_order_pay_confirm_desc);
            case GOODS_EXECUTE_STATUS_PENDING_SEND:
                return context.getString(R.string.status_goods_order_send_desc);
            case GOODS_EXECUTE_STATUS_PENDING_RECEIVE:
                return context.getString(R.string.status_goods_order_receive_desc);
            case GOODS_EXECUTE_STATUS_COMPLETED:
                return context.getString(R.string.status_goods_order_completed_desc);
            case GOODS_EXECUTE_STATUS_CANCEL:
                return context.getString(R.string.status_goods_order_cancel_desc);
        }
        return "";
    }

    /**
     * 是否可以再次预定
     */
    public static boolean isCanBuyAgain(int status) {
        if (status == GOODS_EXECUTE_STATUS_CANCEL
                || status == GOODS_EXECUTE_STATUS_COMPLETED) {
            return true;
        }
        return false;
    }


    @SuppressWarnings("ConstantConditions")
    public static String getGrainOrderStatusMsg(Context context, int status) {
        switch (status) {
            case GRAIN_ORDER_STATUS_PENDING_REPORTED:
                return context.getString(R.string.status_grain_order_report);
            case GRAIN_ORDER_STATUS_PENDING_CONFIRM:
                return context.getString(R.string.status_grain_order_confirm);
            case GRAIN_ORDER_STATUS_COMPLETED:
                return context.getString(R.string.status_grain_order_completed);
            case GRAIN_ORDER_STATUS_CANCEL:
                return context.getString(R.string.status_grain_order_cancel);
        }
        return "";
    }

    @SuppressWarnings("ConstantConditions")
    public static String getGrainOrderStatusTitle(Context context, int status) {
        switch (status) {
            case GRAIN_ORDER_STATUS_PENDING_REPORTED:
                return context.getString(R.string.status_grain_order_report_title);
            case GRAIN_ORDER_STATUS_PENDING_CONFIRM:
                return context.getString(R.string.status_grain_order_confirm_title);
            case GRAIN_ORDER_STATUS_COMPLETED:
                return context.getString(R.string.status_grain_order_completed_title);
            case GRAIN_ORDER_STATUS_CANCEL:
                return context.getString(R.string.status_grain_order_cancel_title);
        }
        return "";
    }

    public static String getGrainOrderStatusDes(Context context, int status) {
        switch (status) {
            case GRAIN_ORDER_STATUS_PENDING_REPORTED:
                return context.getString(R.string.status_grain_order_report_desc);
            case GRAIN_ORDER_STATUS_PENDING_CONFIRM:
                return context.getString(R.string.status_grain_order_confirm_desc);
            case GRAIN_ORDER_STATUS_COMPLETED:
                return context.getString(R.string.status_grain_order_completed_desc);
            case GRAIN_ORDER_STATUS_CANCEL:
                return context.getString(R.string.status_grain_order_cancel_desc);
        }
        return "";
    }

    /**
     * 用户是否可以取消粮食订单
     */
    public static boolean grainOrderCanCancel() {
        return false;
    }


    /**
     * 获取运送方式价格
     */
    public static String getDeliveryPriceType(Context context, int delivery) {
        if (delivery == DELIVERY_TYPE_SELF) {
            return context.getString(R.string.txt_self_price);
        }
        return context.getString(R.string.txt_drop_in_price);
    }

    public static String getCooperativeSignedStatus(Context context, int status) {
        if (status == COOPERATIVE_SIGNED_STATUS_UNSIGNED) {
            return context.getString(R.string.sign_status_signed_pending);
        } else if (status == COOPERATIVE_SIGNED_STATUS_SIGNED || status == COOPERATIVE_SIGNED_STATUS_RESCIND_CHECK) {
            return context.getString(R.string.sign_status_signed_signed);
        } else if (status == COOPERATIVE_SIGNED_STATUS_FAIL) {
            return context.getString(R.string.sign_status_signed_fail);
        } else if (status == COOPERATIVE_SIGNED_STATUS_RESCIND) {
            return context.getString(R.string.sign_status_signed_rescind);
        }
        return "";
    }

    /**
     * 合作社是否签约
     */
    public static boolean cooperativeIsSigned(int status) {
        if (status == COOPERATIVE_SIGNED_STATUS_SIGNED || status == COOPERATIVE_SIGNED_STATUS_RESCIND_CHECK) {
            return true;
        }
        return false;
    }

    /**
     * 是否可以重新申请合作社
     */
    public static boolean canCancelApply(int status) {
        if (status == COOPERATIVE_SIGNED_STATUS_UNSIGNED) {
            return true;
        }
        return false;
    }

    /**
     * 是否可以重新申请合作社
     */
    public static boolean canApply(int status) {
        if (status == COOPERATIVE_SIGNED_STATUS_FAIL || status == COOPERATIVE_SIGNED_STATUS_RESCIND) {
            return true;
        }
        return false;
    }


    /**
     * 获取运送方式地址
     */
    public static String getDeliveryAddress(Context context, int delivery) {
        if (delivery == DELIVERY_TYPE_SELF) {
            return context.getString(R.string.format_buyer_address);
        }
        if (delivery == DataSetting.DELIVERY_TYPE_DROP_IN ) {
            return context.getString(R.string.format_cooperative_address);
        }
        return context.getString(R.string.format_warehouse_address);
    }

    /**
     * 获取运送方式
     */
    public static String getDeliveryStr(Context context, int delivery) {
        if (delivery == DELIVERY_TYPE_SELF) {
            return context.getString(R.string.txt_delivery_self);
        }
        return context.getString(R.string.txt_delivery_drop_in);
    }


    /**
     * 计算提现手续费
     */
    public static double calcWithdrawnFee(double price) {
        return price * 0.01;
    }

    /**
     * 获取银行卡类型
     */
    public static String getBankCardType(Context context, int type) {
        if (type == 1) {
            return context.getString(R.string.txt_bank_card_type_self);
        } else if (type == 2) {
            return context.getString(R.string.txt_bank_card_type_business);
        }
        return "";
    }

    /**
     * 获取贷款状态文本
     */
    public static String getLoanStatus(Context context, int status) {
        if (status == LOAN_STATUS_APPLY) {
            return context.getString(R.string.loan_status_pending);
        } else if (status == LOAN_STATUS_CHECK) {
            return context.getString(R.string.loan_status_checked);
        } else if (status == LOAN_STATUS_BANK_CHECK) {
            return context.getString(R.string.loan_status_checked);
        } else if (status == LOAN_STATUS_PENDING_LOAN) {
            return context.getString(R.string.loan_status_checked);
        } else if (status == LOAN_STATUS_FUND) {
            return context.getString(R.string.loan_status_fund);
        } else if (status == LOAN_STATUS_REFUND) {
            return context.getString(R.string.loan_status_refund);
        } else if (status == LOAN_STATUS_BAD) {
            return context.getString(R.string.loan_status_bad);
        }

        return "";
    }

    /**
     * 获取账单类型
     */
    public static String getBillType(Context context, int type) {
        switch (type) {
            case BILL_LOG_TYPE_WITHDRAW:
                return context.getString(R.string.txt_withdraw_deposit);
            case BILL_LOG_TYPE_TOP_UP:
                return context.getString(R.string.txt_top_up);
            case BILL_LOG_TYPE_BUY:
                return context.getString(R.string.txt_buy_goods);
            case BILL_LOG_TYPE_SELL:
                return context.getString(R.string.txt_sell_grain);
            case BILL_LOG_TYPE_REFUND:
                return context.getString(R.string.txt_refund);
            case BILL_LOG_TYPE_OTHER:
                return context.getString(R.string.txt_other);
        }
        return "";
    }

    /**
     * 获取账单状态
     * 1-未完成，2-已完成，3-审核失败
     */
    public static String getBillStatus(Context context, int status) {
        if (status == 1) {
            return context.getString(R.string.txt_unfinished);
        } else if (status == 2) {
            return context.getString(R.string.txt_completed);
        } else if (status == 3) {
            return context.getString(R.string.txt_sign_failed);
        }
        return "";
    }

    /**
     * 获取流水类型
     */
    public static String getBillSerialType(Context context, double money) {
        if (money < 0) {
            return context.getString(R.string.txt_outlay);
        }

        return context.getString(R.string.txt_income);
    }

    /**
     * 生成加密处理的银行卡号
     */
    public static String getCoverBankCard(String bankCard) {
        if (TextUtils.isEmpty(bankCard)) {
            return bankCard;
        } else if (bankCard.length() > 8) {
            int length = bankCard.length();
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < length; i++) {
                if (i != 0 && i % 4 == 0) {
                    builder.append(" ");
                }

                if (i / 4 == 0) {    //开头一段
                    builder.append(bankCard.charAt(i));
                } else if ((i + 4) / 4 == (length + 3) / 4) {//最后一段
                    builder.append(bankCard.charAt(i));
                } else {
                    builder.append("*");
                }

            }
            return builder.toString();
        }
        return bankCard;
    }

    /**
     * 生成加密处理的手机号
     */
    @SuppressWarnings("StringBufferReplaceableByString")
    public static String getCoverPhone(String phone) {
        if (TextUtils.isEmpty(phone)) {
            return phone;
        } else if (phone.length() >= 11) {
            StringBuilder phoneBuilder = new StringBuilder(phone.substring(0, 3));
            phoneBuilder.append("*****");
            phoneBuilder.append(phone.substring(8));
            return phoneBuilder.toString();
        }
        return phone;
    }

}
